package com.example.wemedia.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.util.R;
import com.example.common.util.ResultCodeRecord;
import com.example.model.wemedia.pojo.Sensitive;
import com.example.wemedia.service.SensitiveService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Tag(name = "敏感词管理")
@RestController
@RequestMapping("sensitive")
public class SensitiveController  {
    @Autowired
    private SensitiveService sensitiveService;

    @GetMapping(value = "/sensitive/getAllSensitive")
    @Operation(summary= "获取所有敏感词信息")
    public R getAllSensitive() {
        List< Sensitive > sensitiveList = sensitiveService.list();
        if (sensitiveList != null) {
            return R.ok("查询所有敏感词成功").data("sensitiveList", sensitiveList);
        } else {
            return R.error("查询所有敏感词失败");
        }
    }

    @PostMapping(value = "/sensitive/addSensitive")
    @Operation(summary = "增加敏感词")
    public R addSensitive(@RequestBody Sensitive sensitive) {
        // 1、检查参数
        if (sensitive == null) {
            return R.error(ResultCodeRecord.PARAM_INVALID);
        }
        LambdaQueryWrapper<Sensitive> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Sensitive::getSensitives, sensitive.getSensitives());
        Sensitive sensitives = sensitiveService.getOne(wrapper);
        if (sensitives != null) {
            return R.error(ResultCodeRecord.DATA_EXIST, "敏感词已存在");
        }
        boolean flag = sensitiveService.save(sensitive);
        sensitive.setCreatedTime(new Date());
        if (flag) {
            return R.ok("增加敏感词成功");
        } else {
            return R.error("增加敏感词失败");
        }
    }

    @DeleteMapping("/sensitive/deleteSensitive/{sensitiveId}")
    @Operation(summary = "删除敏感词")
    public R deleteSensitive(@PathVariable(value = "sensitiveId") Integer sensitiveId) {
        // 1、检查参数
        if (sensitiveId == null) {
            return R.error(ResultCodeRecord.PARAM_INVALID);
        }
        // 2、查询敏感词
        Sensitive sensitive = sensitiveService.getById(sensitiveId);
        if (sensitive == null) {
            return R.error(ResultCodeRecord.DATA_NOT_EXIST, "敏感词不存在");
        }
        boolean flag = sensitiveService.removeById(sensitiveId);
        if (flag) {
            return R.ok("删除敏感词成功");
        } else {
            return R.error("删除敏感词失败");
        }
    }

    @GetMapping("/sensitive/getBySensitiveId/{sensitiveId}")
    @Operation(summary = "按照敏感词编号查看")
    public R getBySensitiveId(@PathVariable(value = "sensitiveId") Integer sensitiveId) {
        Sensitive sensitive = sensitiveService.getById(sensitiveId);
        if (sensitive != null) {
            return R.ok("查询敏感词成功").data("sensitive", sensitive);
        } else {
            return R.error("查询敏感词失败");
        }
    }

    @PutMapping("/sensitive/updateSensitive")
    @Operation(summary = "修改敏感词")
    public R updateSensitive(@RequestBody Sensitive sensitive) {
        // 1、检查参数
        if (sensitive == null || sensitive.getId() == null) {
            return R.error(ResultCodeRecord.PARAM_INVALID);
        }
        boolean flag = sensitiveService.updateById(sensitive);
        if (flag) {
            return R.ok("修改敏感词成功");
        } else {
            return R.error("修改敏感词失败");
        }
    }

    @PostMapping("/sensitive/getSensitivePage/{page}/{pageSize}")
    @Operation(summary = "查询模糊敏感词分页")
    public R getSensitivePage(@RequestBody String sensitiveName, @PathVariable(value = "page") Long page, @PathVariable(value = "pageSize") Long pageSize) {
        // 1、 检查参数是否有效
        if (sensitiveName == null) {
            return R.error(ResultCodeRecord.PARAM_INVALID);
        }
        // 2、分页查询
        Page< Sensitive > sensitivePage = new Page<>(page, pageSize);
        // 3、条件构造器
        LambdaQueryWrapper< Sensitive > wrapper = new LambdaQueryWrapper<>();
        // 模糊敏感词查询
        if (StringUtils.isNotBlank(sensitiveName)) {
            wrapper.like(Sensitive::getSensitives, sensitiveName);
        }
        // 排序
        wrapper.orderByDesc(Sensitive::getCreatedTime);
        // 查询数据
        sensitiveService.page(sensitivePage, wrapper);
        // 每页的数据集合
        List< Sensitive > sensitiveList = sensitivePage.getRecords();
        // 总的记录数
        Long total = sensitivePage.getTotal();
        //每页条数
        Long size = sensitivePage.getSize();
        // 总页数
        Long pages = sensitivePage.getPages();
        Map< String, Object > map = new HashMap<>();
        map.put("total", total);
        map.put("pageSize", size);
        map.put("pages", pages);
        map.put("page", page);
        map.put("sensitiveList", sensitiveList);
        if (map != null) {
            return R.ok("查询模糊敏感词分页成功").data(map);
        } else {
            return R.error("查询模糊敏感词分页失败");
        }
    }
}

